<?php
/**
 * 权限管理控制器
 * @author juntao
 *
 */
class AuthController extends App_Zend_Controller_Action_Admin
{
	/**该方法主要进行的是权限判断*/
	public function init() 
	{
		$user = App_User::getCurrentUser();
		if($user){
			if(!in_array('6', json_decode($user->permission))){
				//$this->_request->setControllerName('error');
				exit('You have no priviage');
			}
		}else{
			$this->_redirect('/user/login');
		}
		parent::init();		
	}
	
	/**用户列表*/
	public function indexAction()
	{
		$user = App_Model::factory('User');
		$userList  = $user->listAll();
		$this->view->userList = $userList;
	}

	/**新增用户*/
	public function addAction()
	{
		if($this->getRequest()->isPost()) {
			$username = $this->getRequest()->getParam('username', '');
			$truename = $this->getRequest()->getParam('truename', '');
			$email = $this->getRequest()->getParam('email', '');
			$password = $this->getRequest()->getParam('password', '');
			$confirmPassword = $this->getRequest()->getParam('confirm_password', '');
			$isAdmin = intval($this->getRequest()->getParam('is_admin','0'));
			$usertype = intval($this->getRequest()->getParam('user_type','0'));
			
			$permission = $this->getRequest()->getParam('permission');
			$user = App_Model::factory('User');
				
			$msg = array();
			if (empty($username)) {
				$msg[] = '用户名不能为空';
			} else {
				if ($user->exist($username)) {
					$msg[] = '用户名已经存在';
				}
			}
			
			if(empty($email)){
				$msg[] = 'QA邮箱不能为空';
			}
			
			if(empty($permission)){
				$msg[] = '权限选择不能为空';
			}
				
			if(empty($password)) {
				$msg[] = '密码不能为空';
			} elseif ($password != $confirmPassword) {
				$msg[] = '密码不相同';
			}
				
			if ($msg) {
				$this->view->msg = $msg;
				$this->view->username = $username;
				$this->view->truename = $truename;
				$this->view->isAdmin = $isAdmin;
				return;
			}
				
			$row = array('user_name'=>$username, 'email'=>$email, 'user_pass'=>md5($password), 'true_name'=>$truename, 'is_admin'=>$isAdmin, 'user_type'=>$usertype, 'permission'=>json_encode($permission), 'add_time'=>date('Y-m-d H:i:s', time()));
				
			if ($user->add($row)) {
				$msg[] = '添加成功';
			} else {
				$msg[] = '添加失败';
			}
			$this->view->msg = $msg;
		}
	}
	
	/**修改用户包含权限*/
	public function updateAction()
	{
		$userid = $this->getRequest()->getParam('id');
		$userModel = App_Model::factory('User');
		$user = $userModel->getOne($userid)->toArray();

		if($this->getRequest()->isPost()) {
			$username = $this->getRequest()->getParam('username', '');
			$truename = $this->getRequest()->getParam('truename', '');
			$email = $this->getRequest()->getParam('email', '');
			$password = $this->getRequest()->getParam('password', '');
			$permission = $this->getRequest()->getParam('permission', '');
			$isAdmin = intval($this->getRequest()->getParam('is_admin','0'));
			$usertype = intval($this->getRequest()->getParam('user_type','0'));
			
			$msg = array();
			if(empty($permission)){
				$msg[] = '权限选择不能为空';
				$this->view->msg = $msg;
				return;
			}
			
			$row = array('true_name'=>$truename, 'user_name'=>$username, 'email'=>$email, 'is_admin'=>$isAdmin, 'user_type'=>$usertype, 'permission'=>json_encode($permission));
				
			if ($userModel->update($row, $user['user_id'])) {
				$msg[] = '修改成功';
				$user['permission'] = json_encode($permission);
			} else {
				$msg[] = '修改失败';
			}
			$this->view->msg = $msg;
		}
		$user = $userModel->getOne($userid)->toArray();
		$this->view->user = $user;
	}
	
	/**删除用户*/
	public function deleteAction()
	{
		$this->getHelper('viewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
	
		$id = $this->getRequest()->getParam('id', 0);
		$id = intval($id);
	
		$user = App_Model::factory('User');
		if ($user->delete($id)) {
			echo 1;
		} else {
			echo 0;
		}
	}
}
